<script setup>
import { ref, reactive, toRaw } from 'vue'
import { service, cache } from '@utils'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refresh'])

const visible = ref(false)
const formRef = ref()

const state = reactive({
    param: {}
})

const rules = {
    lostMoney: [{ required: true, message: '请输入', trigger: 'blur' }],
    damageMoney: [{ required: true, message: '请输入', trigger: 'blur' }],
    violateMoney: [{ required: true, message: '请输入', trigger: 'blur' }]
}

const open = (item) => {
    state.param = { ...item }
    visible.value = true
}

const close = () => {
    visible.value = false
    state.param = {}
}

const sure = async () => {
    formRef.value.validate((valid) => {
        if (valid) {
            realSubmit()
        }
    })
}
const realSubmit = async () => {
    const res = await service.bfqz.orderDetailUpdate(toRaw(state.param))
    if (res) {
        ElMessage.success('修改成功')
        close()
        emit('refresh')
    }
}
//把方法暴露给父组件
defineExpose({
    open
})
</script>
    
<template>
    <el-dialog v-model="visible" :title="!!state.param.id ? '修改' : '新增'" center :close-on-click-modal="false" @close="close"
        destroy-on-close :width="450">
        <section>
            <el-form :model="state.param" :rules="rules" ref="formRef" label-width="100px" label-suffix=":" onsubmit="return false;">
                <el-form-item label="丢失赔偿" prop="lostMoney">
                    <el-input-number v-model="state.param.lostMoney" placeholder="请输入丢失赔偿" style="width: 100%" :min="0" :max="99999"
                        clearable />
                </el-form-item>
                <el-form-item label="损坏赔偿" prop="damageMoney">
                    <el-input-number v-model="state.param.damageMoney" placeholder="请输入损坏赔偿" style="width: 100%" :min="0" :max="99999"
                        clearable />
                </el-form-item>
                <el-form-item label="违约赔偿" prop="violateMoney">
                    <el-input-number v-model="state.param.violateMoney" placeholder="请输入违约赔偿" style="width: 100%" :min="0" :max="99999"
                        clearable />
                </el-form-item>
                <el-form-item label="结算说明" prop="settleRemark">
                    <el-input v-model="state.param.settleRemark" type="textarea" placeholder="请输入结算说明" :rows="3" :maxlength="150"
                        show-word-limit clearable></el-input>
                </el-form-item>
            </el-form>
        </section>

        <template #footer>
            <span class="flex-align-end">
                <div class="margin-right20">
                    <el-button @click="close">取消</el-button>
                </div>
                <el-button type="primary" @click="sure">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
    